package indexing;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class IndexSentence {
	
	private WhitespaceAnalyzer analyzer_1;
	private FSDirectory dir_1;
	private IndexWriterConfig config_1;
	private IndexWriter w_1;
	private Document doc;
	//splitter patterns
	private static Pattern pLineSplit=Pattern.compile("\n");
	private static Pattern pfieldSplit=Pattern.compile("\t");
	private static Pattern pTagParticleSplit=Pattern.compile("/");
	private static Pattern pPartInfoSplit=Pattern.compile("|");
	//Attributes used in the method "preparedInfofForIndex()"
	private String id;
	private String adjectives;
	private String adverbs;
	private String nouns;
	private String untagSent;
	private String [] instanceSet;
	private String [] fieldSet;
	private String [] particleSet;
	private String [] partInfoSet;
	private int instCont;
	private int fieldCont;
	private int partCont;
	/**
	 * Method called to initialize the attributes of the class
	 * @param String indexDir Main directory where the folder index_s will be stored- 
	 */
	public void init(String indexDir)throws Exception{
		/**
		 * Example of use of PerFieldAnalyzer:
		 * 	http://lucene.472066.n3.nabble.com/PerFieldAnalyzerWrapper-use-Analyzer-s-not-being-used-as-expected-td541413.html
		 * new way
		 * 	http://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/core/org/apache/lucene/analysis/PerFieldAnalyzerWrapper.html
		 */
		this.analyzer_1 = new WhitespaceAnalyzer(Version.LUCENE_35);
		if(System.getProperty("os.name").toLowerCase().indexOf("win")>=0)
			this.dir_1 = FSDirectory.open(new File(indexDir+"\\index_s"));
		else
			this.dir_1 = FSDirectory.open(new File(indexDir+"/index_s"));
		this.config_1 = new IndexWriterConfig(Version.LUCENE_35,analyzer_1);
		this.w_1=new IndexWriter(this.dir_1, this.config_1);
	}
	
	public void closeIndexWriter()throws IOException{
		w_1.close();
	}
	/**
	 * Create a new document in the Index_s structure and add the specified fields
	 * @param String f_id,String f_adj,String f_adv,String f_noun,String f_sentence,String f_tagSenten
	 */
	public void addDocIndex_1(String f_id,String f_adj,String f_adv,String f_noun,String f_sentence,String f_tagSenten) throws IOException {
	    this.doc= new Document();
	    this.doc.add((Fieldable) new Field("id", f_id, Field.Store.YES, Field.Index.ANALYZED));
	    this.doc.add((Fieldable) new Field("adj", f_adj, Field.Store.YES, Field.Index.ANALYZED));
	    this.doc.add((Fieldable) new Field("adv", f_adv, Field.Store.YES, Field.Index.ANALYZED));
	    this.doc.add((Fieldable) new Field("sentence", f_sentence, Field.Store.YES, Field.Index.NO));
	    this.doc.add((Fieldable) new Field("tagged_sentence", f_tagSenten, Field.Store.YES, Field.Index.NO));
	    this.w_1.addDocument(this.doc);
	 }
	/**
	 * Receives a information block which is composed by a number of sentences. Then process this block
	 * and index it with a defined logic.
	 * @param String information
	 */
	public void generateInfoIndex(String information) throws IOException{
		/**
		 * Check this link, page 3, to understand the tags meaning
		 *  http://jones.ling.indiana.edu/~mdickinson/09/615/slides/05-pos-syntax-2x3.pdf
		 */
		this.instanceSet=this.pLineSplit.split(information);
		for(this.instCont=0;this.instCont<this.instanceSet.length;this.instCont++){
			//Tasks performed per line
			this.fieldSet=this.pfieldSplit.split(this.instanceSet[this.instCont]);
			this.id="";
			this.adjectives="";
			this.adverbs="";
			this.nouns="";
			this.untagSent="";
			for(this.fieldCont=0;this.fieldCont<this.fieldSet.length;this.fieldCont++){
				//Tasks performed per field
				if(this.fieldCont<3){
					this.id+=this.fieldSet[this.fieldCont];
				}else{
					//The tagged sentence field
					this.particleSet=this.pTagParticleSplit.split(this.fieldSet[this.fieldCont]);
					for(this.partCont=0;this.partCont<this.particleSet.length;this.partCont++){
						//Task performed per tagged particle which belong to a given sentence or instance
						this.partInfoSet=this.pPartInfoSplit.split(this.particleSet[this.partCont]);
						//Task performed in each of the fields that stored the information about a certain particle
						//With the current configuration there are three part "particle|tag|probability"
						if(this.partInfoSet[1].contains("JJ"))//Is an Adjective Tag
							this.adjectives+=this.partInfoSet[0]+" ";
						if(this.partInfoSet[1].contains("RB"))//Is an Adverb Tag
							this.adverbs+=this.partInfoSet[0]+" ";
						if(this.partInfoSet[1].contains("NN")||this.partInfoSet[1].contains("NP"))//Is an Adjective Tag
							this.adjectives+=this.partInfoSet[0]+" ";
						this.untagSent+=this.partInfoSet[0]+" ";
					}
					//In case the fields are empty
					if(this.adjectives.isEmpty())
						this.adjectives="VACIO";
					if(this.adverbs.isEmpty())
						this.adverbs="VACIO";
					if(this.nouns.isEmpty())
						this.nouns="VACIO";
					//Store instances information in the Index Structure I
					addDocIndex_1(this.id,this.adjectives,this.adverbs,this.nouns,this.untagSent,this.fieldSet[this.fieldSet.length-1]);
				}
			}
		}
	}
}
